<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li class="active"><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. MongoDB简介">1. MongoDB简介</a></li><li><a href="#t12. Mongoose是什么？">2. Mongoose是什么？</a></li><li><a href="#t23. 使用 mongoose">3. 使用 mongoose</a><ul><li><a href="#t33.1 安装mongoose">3.1 安装mongoose</a></li><li><a href="#t43.2 使用mongoose">3.2 使用mongoose</a></li><li><a href="#t53.3 使用mongoose">3.3 使用mongoose</a></li><li><a href="#t63.4 Schema">3.4 Schema</a><ul><li><a href="#t73.4.1 Schema.Type">3.4.1 Schema.Type</a></li><li><a href="#t83.4.2 定义Schema">3.4.2 定义Schema</a></li><li><a href="#t93.4.3 Model">3.4.3 Model</a></li><li><a href="#t103.4.4 Entity简述">3.4.4 Entity简述</a></li><li><a href="#t113.4.5 保存Entity">3.4.5 保存Entity</a></li><li><a href="#t123.4.6 ObjectId">3.4.6 ObjectId</a></li></ul></li><li><a href="#t133.5 基础操作">3.5 基础操作</a><ul><li><a href="#t143.5.1 查询">3.5.1 查询</a></li><li><a href="#t153.5.2 Model保存">3.5.2 Model保存</a></li><li><a href="#t163.5.3 Entity保存">3.5.3 Entity保存</a></li><li><a href="#t173.5.4 更新">3.5.4 更新</a></li><li><a href="#t183.5.5 删除">3.5.5 删除</a></li><li><a href="#t193.5.6 基本查询">3.5.6 基本查询</a><ul><li><a href="#t203.5.6.1  准备数据">3.5.6.1  准备数据</a></li><li><a href="#t213.5.6.2  属性过滤">3.5.6.2  属性过滤</a></li><li><a href="#t223.5.6.3  findOne(查询单条)">3.5.6.3  findOne(查询单条)</a></li><li><a href="#t233.5.6.4  findById(按ID单条数据)">3.5.6.4  findById(按ID单条数据)</a></li><li><a href="#t243.5.6.5  $gt、$lt(大于、小于)">3.5.6.5  $gt、$lt(大于、小于)</a></li><li><a href="#t253.5.6.6 $ne(不等于)">3.5.6.6 $ne(不等于)</a></li><li><a href="#t263.5.6.7 $in(包含)">3.5.6.7 $in(包含)</a></li><li><a href="#t273.5.6.8 $or(或者)">3.5.6.8 $or(或者)</a></li><li><a href="#t283.5.6.9 $exists(是否存在)">3.5.6.9 $exists(是否存在)</a></li></ul></li><li><a href="#t293.5.7 高级查询">3.5.7 高级查询</a><ul><li><a href="#t303.5.7.1 limit(限制数量)">3.5.7.1 limit(限制数量)</a></li><li><a href="#t313.5.7.2 skip(跳过/略过的数量)">3.5.7.2 skip(跳过/略过的数量)</a></li><li><a href="#t323.5.7.3 sort函数">3.5.7.3 sort函数</a></li><li><a href="#t333.5.7.4 分页查询">3.5.7.4 分页查询</a></li><li><a href="#t343.5.7.5 populate">3.5.7.5 populate</a></li></ul></li></ul></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. MongoDB&#x7B80;&#x4ECB;">1. MongoDB&#x7B80;&#x4ECB; <a href="#t01. MongoDB&#x7B80;&#x4ECB;"> # </a></h2>
<ul>
<li>Mongodb&#x662F;&#x4E00;&#x4E2A;&#x5F00;&#x6E90;&#x7684;NoSQL&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x76F8;&#x6BD4;MySQL&#x90A3;&#x6837;&#x7684;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x5B83;&#x66F4;&#x663E;&#x5F97; &#x8F7B;&#x5DE7;&#x3001;&#x7075;&#x6D3B;&#xFF0C;&#x975E;&#x5E38;&#x9002;&#x5408;&#x5728;&#x6570;&#x636E;&#x89C4;&#x6A21;&#x5F88;&#x5927;&#x3001;&#x4E8B;&#x548C;&#x6027;&#x4E0D;&#x5F3A;&#x7684;&#x573A;&#x5408;&#x4E0B;&#x4F7F;&#x7528;&#x3002;</li>
<li>&#x540C;&#x65F6;&#x5B83;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x6CA1;&#x6709;&#x8868;&#x548C;&#x884C;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x4E5F;&#x6CA1;&#x6709;&#x56FA;&#x5B9A;&#x7684;&#x6A21;&#x5F0F;&#x548C;&#x7ED3;&#x6784;&#xFF0C;&#x6240;&#x6709;&#x7684;&#x6570;&#x636E;&#x90FD;&#x662F;&#x4EE5;&#x6587;&#x6863;&#x7684;&#x5F62;&#x5F0F;&#x5B58;&#x5B58;&#x50A8;(&#x6587;&#x6863;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5173;&#x8054;&#x6570;&#x7EC4;&#x5F0F;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x7684;&#x5185;&#x90E8;&#x7531;&#x5C5E;&#x6027;&#x7EC4;&#x6210;&#xFF0C;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x5BF9;&#x5E94;&#x7684;&#x503C;&#x53EF;&#x80FD;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x65E5;&#x671F;&#x3001;&#x6570;&#x7EC4;&#x3001;&#x751A;&#x81F3;&#x662F;&#x4E00;&#x4E2A;&#x5D4C;&#x5957;&#x6587;&#x6863;)&#xFF0C;&#x6570;&#x636E;&#x683C;&#x5F0F;&#x5C31;&#x662F;JSON&#x3002;</li>
</ul>
<h2 id="t12. Mongoose&#x662F;&#x4EC0;&#x4E48;&#xFF1F;">2. Mongoose&#x662F;&#x4EC0;&#x4E48;&#xFF1F; <a href="#t12. Mongoose&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"> # </a></h2>
<ul>
<li>Mongoose&#x662F;MongoDB&#x7684;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x6A21;&#x578B;&#x5DE5;&#x5177;</li>
<li>&#x540C;&#x65F6;&#x5B83;&#x4E5F;&#x662F;&#x9488;&#x5BF9;MongoDB&#x64CD;&#x4F5C;&#x7684;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x6A21;&#x578B;&#x5E93;,&#x5C01;&#x88C5;&#x4E86;MongoDB&#x5BF9;&#x6587;&#x6863;&#x7684;&#x7684;&#x4E00;&#x4E9B;&#x589E;&#x5220;&#x6539;&#x67E5;&#x7B49;&#x5E38;&#x7528;&#x65B9;&#x6CD5;</li>
<li>&#x8BA9;NodeJS&#x64CD;&#x4F5C;Mongodb&#x6570;&#x636E;&#x5E93;&#x53D8;&#x5F97;&#x66F4;&#x52A0;&#x7075;&#x6D3B;&#x7B80;&#x5355;</li>
<li>Mongoose&#x56E0;&#x4E3A;&#x5C01;&#x88C5;&#x4E86;MongoDB&#x5BF9;&#x6587;&#x6863;&#x64CD;&#x4F5C;&#x7684;&#x5E38;&#x7528;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x9AD8;&#x6548;&#x5904;&#x7406;mongodb,&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x7C7B;&#x4F3C;Schema&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5982;hook&#x3001;plugin&#x3001;virtual&#x3001;populate&#x7B49;&#x673A;&#x5236;&#x3002;</li>
<li>&#x5B98;&#x7F51; <a href="http://mongoosejs.com/">mongoosejs</a></li>
</ul>
<h2 id="t23. &#x4F7F;&#x7528; mongoose">3. &#x4F7F;&#x7528; mongoose <a href="#t23. &#x4F7F;&#x7528; mongoose"> # </a></h2>
<h3 id="t33.1 &#x5B89;&#x88C5;mongoose">3.1 &#x5B89;&#x88C5;mongoose <a href="#t33.1 &#x5B89;&#x88C5;mongoose"> # </a></h3>
<pre><code class="lang-js">$ npm install mongoose
</code></pre>
<h3 id="t43.2 &#x4F7F;&#x7528;mongoose">3.2 &#x4F7F;&#x7528;mongoose <a href="#t43.2 &#x4F7F;&#x7528;mongoose"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> mongoose = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;mongoose&quot;</span>);
<span class="hljs-keyword">var</span> db = mongoose.connect(<span class="hljs-string">&quot;mongodb://user:pass@ip:port/database&quot;</span>);
</code></pre>
<ul>
<li>user &#x7528;&#x6237;&#x540D;</li>
<li>pass &#x5BC6;&#x7801;</li>
<li>ip IP&#x5730;&#x5740;</li>
<li>port &#x7AEF;&#x53E3;&#x53F7;</li>
<li>database &#x6570;&#x636E;&#x5E93;</li>
</ul>
<h3 id="t53.3 &#x4F7F;&#x7528;mongoose">3.3 &#x4F7F;&#x7528;mongoose <a href="#t53.3 &#x4F7F;&#x7528;mongoose"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> mongoose = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;mongoose&apos;</span>);
<span class="hljs-keyword">var</span> connection = mongoose.createConnection(<span class="hljs-string">&quot;mongodb://127.0.0.1/zfpx&quot;</span>);
connection.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;&#x5931;&#x8D25;: &apos;</span> + error);
});
connection.on(<span class="hljs-string">&apos;open&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;&#x6210;&#x529F;&apos;</span>);
});
</code></pre>
<h3 id="t63.4 Schema">3.4 Schema <a href="#t63.4 Schema"> # </a></h3>
<p>Schema&#x662F;&#x6570;&#x636E;&#x5E93;&#x96C6;&#x5408;&#x7684;&#x6A21;&#x578B;&#x9AA8;&#x67B6;
&#x5B9A;&#x4E49;&#x4E86;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x5B57;&#x6BB5;&#x7684;&#x540D;&#x79F0;&#x548C;&#x7C7B;&#x578B;&#x4EE5;&#x53CA;&#x9ED8;&#x8BA4;&#x503C;&#x7B49;&#x4FE1;&#x606F;</p>
<h4 id="t73.4.1 Schema.Type">3.4.1 Schema.Type <a href="#t73.4.1 Schema.Type"> # </a></h4>
<p>NodeJS&#x4E2D;&#x7684;&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x90FD;&#x5C5E;&#x4E8E; Schema.Type
&#x53E6;&#x5916;Mongoose&#x8FD8;&#x5B9A;&#x4E49;&#x4E86;&#x81EA;&#x5DF1;&#x7684;&#x7C7B;&#x578B;
&#x57FA;&#x672C;&#x5C5E;&#x6027;&#x7C7B;&#x578B;&#x6709;:</p>
<ul>
<li>&#x5B57;&#x7B26;&#x4E32;(String)</li>
<li>&#x65E5;&#x671F;&#x578B;(Date)</li>
<li>&#x6570;&#x503C;&#x578B;(Number)</li>
<li>&#x5E03;&#x5C14;&#x578B;(Boolean)</li>
<li>null</li>
<li>&#x6570;&#x7EC4;([])</li>
<li>&#x5185;&#x5D4C;&#x6587;&#x6863;</li>
</ul>
<h4 id="t83.4.2 &#x5B9A;&#x4E49;Schema">3.4.2 &#x5B9A;&#x4E49;Schema <a href="#t83.4.2 &#x5B9A;&#x4E49;Schema"> # </a></h4>
<pre><code class="lang-js"> <span class="hljs-keyword">var</span> personSchema = <span class="hljs-keyword">new</span> Schema({
      <span class="hljs-attr">name</span>:<span class="hljs-built_in">String</span>, <span class="hljs-comment">//&#x59D3;&#x540D;</span>
      binary:Buffer,<span class="hljs-comment">//&#x4E8C;&#x8FDB;&#x5236;</span>
      living:<span class="hljs-built_in">Boolean</span>,<span class="hljs-comment">//&#x662F;&#x5426;&#x6D3B;&#x7740;</span>
      birthday:<span class="hljs-built_in">Date</span>,<span class="hljs-comment">//&#x751F;&#x65E5;</span>
      age:<span class="hljs-built_in">Number</span>,<span class="hljs-comment">//&#x5E74;&#x9F84;</span>
      _id:Schema.Types.ObjectId,  <span class="hljs-comment">//&#x4E3B;&#x952E;</span>
      _fk:Schema.Types.ObjectId,  <span class="hljs-comment">//&#x5916;&#x952E;</span>
      array:[],<span class="hljs-comment">//&#x6570;&#x7EC4;</span>
      arrOfString:[<span class="hljs-built_in">String</span>],<span class="hljs-comment">//&#x5B57;&#x7B26;&#x4E32;&#x6570;&#x7EC4;</span>
      arrOfNumber:[<span class="hljs-built_in">Number</span>],<span class="hljs-comment">//&#x6570;&#x5B57;&#x6570;&#x7EC4;</span>
      arrOfDate:[<span class="hljs-built_in">Date</span>],<span class="hljs-comment">//&#x65E5;&#x671F;&#x6570;&#x7EC4;</span>
      arrOfBuffer:[Buffer],<span class="hljs-comment">//Buffer&#x6570;&#x7EC4;</span>
      arrOfBoolean:[<span class="hljs-built_in">Boolean</span>],<span class="hljs-comment">//&#x5E03;&#x5C14;&#x503C;&#x6570;&#x7EC4;</span>
      arrOfObjectId:[Schema.Types.ObjectId]<span class="hljs-comment">//&#x5BF9;&#x8C61;ID&#x6570;&#x7EC4;</span>
      nested:{ <span class="hljs-comment">//&#x5185;&#x5D4C;&#x6587;&#x6863;</span>
        name:<span class="hljs-built_in">String</span>,
      }
    });

    <span class="hljs-keyword">let</span> p = <span class="hljs-keyword">new</span> Person();
    p.name= <span class="hljs-string">&apos;zfpx&apos;</span>;
    p.age = <span class="hljs-number">25</span>;
    p.birthday = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();
    p.married = <span class="hljs-literal">false</span>;
    p.mixed= {<span class="hljs-attr">any</span>:{<span class="hljs-attr">other</span>:<span class="hljs-string">&apos;other&apos;</span>}};
    p._otherId = <span class="hljs-keyword">new</span> mongoose.Types.ObjectId;
    p.hobby.push(<span class="hljs-string">&apos;smoking&apos;</span>);
    p.ofString.push(<span class="hljs-string">&apos;string&apos;</span>);
    p.ofNumber.pop(<span class="hljs-number">3</span>);
    p.ofDates.addToSet(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>);
    p.ofBuffer.pop();
    p.ofMixed = [<span class="hljs-string">&apos;anything&apos;</span>,<span class="hljs-number">3</span>,{<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx&apos;</span>}];
    p.nested.name = <span class="hljs-string">&apos;zfpx&apos;</span>;
</code></pre>
<h4 id="t93.4.3 Model">3.4.3 Model <a href="#t93.4.3 Model"> # </a></h4>
<p>Model&#x662F;&#x7531;&#x901A;&#x8FC7;Schema&#x6784;&#x9020;&#x800C;&#x6210;
&#x9664;&#x4E86;&#x5177;&#x6709;Schema&#x5B9A;&#x4E49;&#x7684;&#x6570;&#x636E;&#x5E93;&#x9AA8;&#x67B6;&#x4EE5;&#x5916;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;
&#x5982;&#x4F55;&#x901A;&#x8FC7;Schema&#x6765;&#x521B;&#x5EFA;Model&#x5462;&#xFF0C;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-js"><span class="hljs-comment">//&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;</span>
mongoose.connect(<span class="hljs-string">&quot;mongodb://123.57.143.189:27017/zfpx&quot;</span>);
<span class="hljs-comment">//&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x8868;&#x793A;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6A21;&#x578B;</span>
<span class="hljs-keyword">var</span> PersonModel = mongoose.model(<span class="hljs-string">&quot;Person&quot;</span>, PersonSchema);
<span class="hljs-comment">// &#x5982;&#x679C;&#x8BE5;Model&#x5DF2;&#x7ECF;&#x5B9A;&#x4E49;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x901A;&#x8FC7;&#x540D;&#x5B57;&#x83B7;&#x53D6;</span>
<span class="hljs-keyword">var</span> PersonModel = mongoose.model(<span class="hljs-string">&apos;Person&apos;</span>);<span class="hljs-comment">//&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x8868;&#x793A;&#x83B7;&#x53D6;&#x5DF2;&#x5B9A;&#x4E49;&#x7684;&#x6A21;&#x578B;</span>
</code></pre>
<p>&#x62E5;&#x6709;&#x4E86;Model&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x5C31;&#x62E5;&#x6709;&#x4E86;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#x7684;&#x80FD;&#x529B;
&#x5728;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x96C6;&#x5408;&#x540D;&#x79F0;&#x7B49;&#x4E8E; &#x6A21;&#x578B;&#x540D;&#x8F6C;&#x5C0F;&#x5199;&#x518D;&#x8F6C;&#x590D;&#x6570;,&#x6BD4;&#x5982; </p>
<blockquote>
<p>Person&gt;person&gt;people,Child&gt;child&gt;children</p>
</blockquote>
<h4 id="t103.4.4 Entity&#x7B80;&#x8FF0;">3.4.4 Entity&#x7B80;&#x8FF0; <a href="#t103.4.4 Entity&#x7B80;&#x8FF0;"> # </a></h4>
<p>&#x901A;&#x8FC7;Model&#x521B;&#x5EFA;&#x7684;&#x5B9E;&#x4F53;&#xFF0C;&#x5B83;&#x4E5F;&#x53EF;&#x4EE5;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;
&#x4F7F;&#x7528;Model&#x521B;&#x5EFA;Entity&#xFF0C;&#x5982;&#x4E0B;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js"> <span class="hljs-keyword">var</span> personEntity = <span class="hljs-keyword">new</span> PersonModel({
     <span class="hljs-attr">name</span> : <span class="hljs-string">&quot;zfpx&quot;</span>,
     <span class="hljs-attr">age</span>  : <span class="hljs-number">6</span>
 });
</code></pre>
<blockquote>
<p>Schema&#x751F;&#x6210;Model&#xFF0C;Model&#x521B;&#x9020;Entity&#xFF0C;Model&#x548C;Entity&#x90FD;&#x53EF;&#x5BF9;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;,&#x4F46;Model&#x6BD4;Entity&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x7684;&#x529F;&#x80FD;&#x66F4;&#x591A; </p>
</blockquote>
<h4 id="t113.4.5 &#x4FDD;&#x5B58;Entity">3.4.5 &#x4FDD;&#x5B58;Entity <a href="#t113.4.5 &#x4FDD;&#x5B58;Entity"> # </a></h4>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> mongoose = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;mongoose&quot;</span>);
mongoose.connect(<span class="hljs-string">&quot;mongodb://127.0.0.1:27017/zfpx&quot;</span>);
<span class="hljs-keyword">var</span> PersonSchema = <span class="hljs-keyword">new</span> mongoose.Schema({
    <span class="hljs-attr">name</span>: {<span class="hljs-attr">type</span>: <span class="hljs-built_in">String</span>},
    <span class="hljs-attr">age</span>: {<span class="hljs-attr">type</span>: <span class="hljs-built_in">Number</span>, <span class="hljs-attr">default</span>: <span class="hljs-number">0</span>}
});
<span class="hljs-keyword">var</span> PersonModel = mongoose.model(<span class="hljs-string">&quot;Person&quot;</span>, PersonSchema);

<span class="hljs-keyword">var</span> PersonEntity = <span class="hljs-keyword">new</span> PersonModel({
    <span class="hljs-attr">name</span>: <span class="hljs-string">&quot;zfpx&quot;</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">6</span>
});

PersonEntity.save(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error, doc</span>) </span>{
    <span class="hljs-keyword">if</span> (error) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;error :&quot;</span> + error);
    } <span class="hljs-keyword">else</span> {
       <span class="hljs-comment">//doc&#x662F;&#x8FD4;&#x56DE;&#x521A;&#x5B58;&#x7684;person&#x5BF9;&#x8C61; </span>
        <span class="hljs-built_in">console</span>.log(doc);
    }
});
</code></pre>
<h4 id="t123.4.6 ObjectId">3.4.6 ObjectId <a href="#t123.4.6 ObjectId"> # </a></h4>
<p>&#x5B58;&#x50A8;&#x5728;mongodb&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6BCF;&#x4E2A;&#x6587;&#x6863;&#x90FD;&#x6709;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x7684;&#x4E3B;&#x952E;_id
&#x8FD9;&#x4E2A;&#x4E3B;&#x952E;&#x540D;&#x79F0;&#x662F;&#x56FA;&#x5B9A;&#x7684;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x662F;mongodb&#x652F;&#x6301;&#x7684;&#x4EFB;&#x4F55;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;ObjectId &#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x7531;&#x7CFB;&#x7EDF;&#x81EA;&#x5DF1;&#x751F;&#x6210;&#xFF0C;&#x4ECE;&#x67D0;&#x79CD;&#x610F;&#x4E49;&#x4E0A;&#x51E0;&#x4E4E;&#x4E0D;&#x4F1A;&#x91CD;&#x590D;
ObjectId&#x662F;&#x4E00;&#x4E2A;12&#x5B57;&#x8282;&#x7684; BSON &#x7C7B;&#x578B;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x6309;&#x7167;&#x5B57;&#x8282;&#x987A;&#x5E8F;&#xFF0C;&#x4F9D;&#x6B21;&#x4EE3;&#x8868;&#xFF1A;</p>
<ul>
<li>4&#x5B57;&#x8282;&#xFF1A;UNIX&#x65F6;&#x95F4;&#x6233;</li>
<li>3&#x5B57;&#x8282;&#xFF1A;&#x8868;&#x793A;&#x8FD0;&#x884C;MongoDB&#x7684;&#x673A;&#x5668;</li>
<li>2&#x5B57;&#x8282;&#xFF1A;&#x8868;&#x793A;&#x751F;&#x6210;&#x6B64;_id&#x7684;&#x8FDB;&#x7A0B;</li>
<li>3&#x5B57;&#x8282;&#xFF1A;&#x7531;&#x4E00;&#x4E2A;&#x968F;&#x673A;&#x6570;&#x5F00;&#x59CB;&#x7684;&#x8BA1;&#x6570;&#x5668;&#x751F;&#x6210;&#x7684;&#x503C;<blockquote>
<p>&#x6BCF;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#x90FD;&#x6709;&#x4E00;&#x4E2A;&#x7279;&#x6B8A;&#x7684;&#x952E;_id&#xFF0C;&#x8FD9;&#x4E2A;&#x952E;&#x5728;&#x6587;&#x6863;&#x6240;&#x5C5E;&#x7684;&#x96C6;&#x5408;&#x4E2D;&#x662F;&#x552F;&#x4E00;&#x7684;&#x3002;</p>
</blockquote>
</li>
</ul>
<h3 id="t133.5 &#x57FA;&#x7840;&#x64CD;&#x4F5C;">3.5 &#x57FA;&#x7840;&#x64CD;&#x4F5C; <a href="#t133.5 &#x57FA;&#x7840;&#x64CD;&#x4F5C;"> # </a></h3>
<h4 id="t143.5.1 &#x67E5;&#x8BE2;">3.5.1 &#x67E5;&#x8BE2; <a href="#t143.5.1 &#x67E5;&#x8BE2;"> # </a></h4>
<p>&#x8BED;&#x6CD5;</p>
<pre><code class="lang-js">Model.find(&#x67E5;&#x8BE2;&#x6761;&#x4EF6;,callback);
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js">Model.find({},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>)</span>{
  <span class="hljs-comment">//&#x82E5;&#x6CA1;&#x6709;&#x5411;find&#x4F20;&#x9012;&#x53C2;&#x6570;&#xFF0C;&#x9ED8;&#x8BA4;&#x7684;&#x662F;&#x663E;&#x793A;&#x6240;&#x6709;&#x6587;&#x6863;</span>
});

Model.find({ <span class="hljs-string">&quot;age&quot;</span>: <span class="hljs-number">6</span> }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error, docs</span>) </span>{
  <span class="hljs-keyword">if</span>(error){
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;error :&quot;</span> + error);
  }<span class="hljs-keyword">else</span>{
    <span class="hljs-built_in">console</span>.log(docs); <span class="hljs-comment">//docs: age&#x4E3A;6&#x7684;&#x6240;&#x6709;&#x6587;&#x6863;</span>
  }
});
</code></pre>
<h4 id="t153.5.2 Model&#x4FDD;&#x5B58;">3.5.2 Model&#x4FDD;&#x5B58; <a href="#t153.5.2 Model&#x4FDD;&#x5B58;"> # </a></h4>
<p>&#x8BED;&#x6CD5;</p>
<pre><code class="lang-js">Model.create(&#x6587;&#x6863;&#x6570;&#x636E;, callback))
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js"> PersonModel.create({ <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx&quot;</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">7</span>}, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,doc</span>)</span>{
    <span class="hljs-keyword">if</span>(error) {
        <span class="hljs-built_in">console</span>.log(error);
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">console</span>.log(doc);
    }
});
<span class="hljs-string">`
</span></code></pre>
<h4 id="t163.5.3 Entity&#x4FDD;&#x5B58;">3.5.3 Entity&#x4FDD;&#x5B58; <a href="#t163.5.3 Entity&#x4FDD;&#x5B58;"> # </a></h4>
<p>&#x8BED;&#x6CD5;</p>
<pre><code class="lang-js">Entity.save(callback))
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> PersonEntity = <span class="hljs-keyword">new</span> PersonModel({<span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx&quot;</span>,<span class="hljs-attr">age</span>: <span class="hljs-number">9</span>});

PersonEntity.save(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,doc</span>) </span>{
   <span class="hljs-keyword">if</span>(error) {
      <span class="hljs-built_in">console</span>.log(error);
   } <span class="hljs-keyword">else</span> {
      <span class="hljs-built_in">console</span>.log(doc);
   }
});
</code></pre>
<h4 id="t173.5.4 &#x66F4;&#x65B0;">3.5.4 &#x66F4;&#x65B0; <a href="#t173.5.4 &#x66F4;&#x65B0;"> # </a></h4>
<p>&#x8BED;&#x6CD5;</p>
<pre><code class="lang-js">Model.update(&#x67E5;&#x8BE2;&#x6761;&#x4EF6;,&#x66F4;&#x65B0;&#x5BF9;&#x8C61;,callback);
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> conditions = {<span class="hljs-attr">name</span> : <span class="hljs-string">&apos;zfpx&apos;</span>};
  <span class="hljs-keyword">var</span> update = {<span class="hljs-attr">$set</span> : { <span class="hljs-attr">age</span> : <span class="hljs-number">100</span> }};
  PersonModel.update(conditions, update, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>)</span>{
      <span class="hljs-keyword">if</span>(error) {
          <span class="hljs-built_in">console</span>.log(error);
      } <span class="hljs-keyword">else</span> {
          <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Update success!&apos;</span>);
        }
    });
</code></pre>
<p>&#x8BF7;&#x6CE8;&#x610F;&#x5982;&#x679C;&#x5339;&#x914D;&#x5230;&#x591A;&#x6761;&#x8BB0;&#x5F55;&#xFF0C;&#x9ED8;&#x8BA4;&#x53EA;&#x66F4;&#x65B0;&#x4E00;&#x6761;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x66F4;&#x65B0;&#x5339;&#x914D;&#x5230;&#x7684;&#x6240;&#x6709;&#x8BB0;&#x5F55;&#x7684;&#x8BDD;&#x9700;&#x8981;&#x52A0;&#x4E00;&#x4E2A;&#x53C2;&#x6570; {multi:true}</p>
<h4 id="t183.5.5 &#x5220;&#x9664;">3.5.5 &#x5220;&#x9664; <a href="#t183.5.5 &#x5220;&#x9664;"> # </a></h4>
<p>&#x8BED;&#x6CD5;</p>
<pre><code class="lang-js">Model.remove(&#x67E5;&#x8BE2;&#x6761;&#x4EF6;,callback);
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> conditions = { <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span> };
PersonModel.remove(conditions, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>)</span>{
    <span class="hljs-keyword">if</span>(error) {
          <span class="hljs-built_in">console</span>.log(error);
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Delete success!&apos;</span>);
    }
});
</code></pre>
<h4 id="t193.5.6 &#x57FA;&#x672C;&#x67E5;&#x8BE2;">3.5.6 &#x57FA;&#x672C;&#x67E5;&#x8BE2; <a href="#t193.5.6 &#x57FA;&#x672C;&#x67E5;&#x8BE2;"> # </a></h4>
<h5 id="t203.5.6.1  &#x51C6;&#x5907;&#x6570;&#x636E;">3.5.6.1  &#x51C6;&#x5907;&#x6570;&#x636E; <a href="#t203.5.6.1  &#x51C6;&#x5907;&#x6570;&#x636E;"> # </a></h5>
<pre><code class="lang-js">        PersonModel.create([
                          { <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx1&quot;</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">1</span> },
                          { <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx2&quot;</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">2</span> },
                          { <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx3&quot;</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">3</span> },
                          { <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx4&quot;</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">4</span> },
                          { <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx5&quot;</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">5</span> },
                          { <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx6&quot;</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">6</span>},
                          { <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx7&quot;</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">7</span> },
                          { <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx8&quot;</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">8</span> },
                          { <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx9&quot;</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">9</span>},
                          { <span class="hljs-attr">name</span>:<span class="hljs-string">&quot;zfpx10&quot;</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">10</span> }
                         ], <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>) </span>{
            <span class="hljs-keyword">if</span>(error) {
                <span class="hljs-built_in">console</span>.log(error);
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;save ok&apos;</span>);
            }
        });
</code></pre>
<h5 id="t213.5.6.2  &#x5C5E;&#x6027;&#x8FC7;&#x6EE4;">3.5.6.2  &#x5C5E;&#x6027;&#x8FC7;&#x6EE4; <a href="#t213.5.6.2  &#x5C5E;&#x6027;&#x8FC7;&#x6EE4;"> # </a></h5>
<p>&#x8BED;&#x6CD5;</p>
<pre><code class="lang-js">find(Conditions,field,callback)
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js"><span class="hljs-comment">//field&#x7701;&#x7565;&#x6216;&#x4E3A;Null&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x3002;</span>
<span class="hljs-comment">//&#x8FD4;&#x56DE;&#x53EA;&#x5305;&#x542B;name&#x3001;age&#x4E24;&#x4E2A;&#x952E;&#x7684;&#x6240;&#x6709;&#x8BB0;&#x5F55;</span>
Model.find({},{<span class="hljs-attr">name</span>:<span class="hljs-number">1</span>, <span class="hljs-attr">age</span>:<span class="hljs-number">1</span>, <span class="hljs-attr">_id</span>:<span class="hljs-number">0</span>}&#xFF0C;<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,docs</span>)</span>{
   <span class="hljs-comment">//docs &#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;</span>
})
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x628A;&#x663E;&#x793A;&#x7684;&#x5C5E;&#x6027;&#x8BBE;&#x7F6E;&#x4E3A;&#x5927;&#x4E8E;&#x96F6;&#x7684;&#x6570;&#x5C31;&#x53EF;&#x4EE5;&#xFF0C;&#x5F53;&#x7136;1&#x662F;&#x6700;&#x597D;&#x7406;&#x89E3;&#x7684;&#xFF0C;_id&#x662F;&#x9ED8;&#x8BA4;&#x8FD4;&#x56DE;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x8981;&#x663E;&#x793A;&#x52A0;&#x4E0A;(&quot;_id&quot;:0)</p>
<h5 id="t223.5.6.3  findOne(&#x67E5;&#x8BE2;&#x5355;&#x6761;)">3.5.6.3  findOne(&#x67E5;&#x8BE2;&#x5355;&#x6761;) <a href="#t223.5.6.3  findOne(&#x67E5;&#x8BE2;&#x5355;&#x6761;)"> # </a></h5>
<p>&#x4E0E;find&#x76F8;&#x540C;&#xFF0C;&#x4F46;&#x53EA;&#x8FD4;&#x56DE;&#x5355;&#x4E2A;&#x6587;&#x6863;&#xFF0C;&#x4E5F;&#x5C31;&#x8BF4;&#x5F53;&#x67E5;&#x8BE2;&#x5230;&#x5373;&#x4E00;&#x4E2A;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x5C06;&#x505C;&#x6B62;&#x7EE7;&#x7EED;&#x67E5;&#x8BE2;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x67E5;&#x8BE2;&#x7ED3;&#x679C; 
&#x8BED;&#x6CD5;</p>
<pre><code class="lang-js">findOne(Conditions,callback)
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js">TestModel.findOne({ <span class="hljs-attr">age</span>: <span class="hljs-number">6</span>}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, doc</span>)</span>{
       <span class="hljs-comment">// &#x67E5;&#x8BE2;&#x7B26;&#x5408;age&#x7B49;&#x4E8E;6&#x7684;&#x7B2C;&#x4E00;&#x6761;&#x6570;&#x636E;</span>
       <span class="hljs-comment">// doc&#x662F;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;</span>
});
</code></pre>
<h5 id="t233.5.6.4  findById(&#x6309;ID&#x5355;&#x6761;&#x6570;&#x636E;)">3.5.6.4  findById(&#x6309;ID&#x5355;&#x6761;&#x6570;&#x636E;) <a href="#t233.5.6.4  findById(&#x6309;ID&#x5355;&#x6761;&#x6570;&#x636E;)"> # </a></h5>
<p>&#x4E0E;findOne&#x76F8;&#x540C;&#xFF0C;&#x4F46;&#x5B83;&#x53EA;&#x63A5;&#x6536;&#x6587;&#x6863;&#x7684;_id&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x5355;&#x4E2A;&#x6587;&#x6863; &#x8BED;&#x6CD5;</p>
<pre><code class="lang-js">findById(_id, callback)
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js">PersonModel.findById(person._id, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, doc</span>)</span>{
     <span class="hljs-comment">//doc &#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x6587;&#x6863;</span>
});
</code></pre>
<h5 id="t243.5.6.5  $gt&#x3001;$lt(&#x5927;&#x4E8E;&#x3001;&#x5C0F;&#x4E8E;)">3.5.6.5  $gt&#x3001;$lt(&#x5927;&#x4E8E;&#x3001;&#x5C0F;&#x4E8E;) <a href="#t243.5.6.5  $gt&#x3001;$lt(&#x5927;&#x4E8E;&#x3001;&#x5C0F;&#x4E8E;)"> # </a></h5>
<p>&#x67E5;&#x8BE2;&#x65F6;&#x6211;&#x4EEC;&#x7ECF;&#x5E38;&#x4F1A;&#x78B0;&#x5230;&#x8981;&#x6839;&#x636E;&#x67D0;&#x4E9B;&#x5B57;&#x6BB5;&#x8FDB;&#x884C;&#x6761;&#x4EF6;&#x7B5B;&#x9009;&#x67E5;&#x8BE2;&#xFF0C;&#x6BD4;&#x5982;&#x8BF4;Number&#x7C7B;&#x578B;&#xFF0C;&#x600E;&#x4E48;&#x529E;&#x5462;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;$gt(&gt;)&#x3001;$lt(&lt;)&#x3001;$lte(&lt;=)&#x3001;$gte(&gt;=)&#x64CD;&#x4F5C;&#x7B26;&#x8FDB;&#x884C;&#x6392;&#x9664;&#x6027;&#x7684;&#x67E5;&#x8BE2;&#xFF0C;&#x5982;&#x4E0B;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js">Model.find({<span class="hljs-string">&quot;age&quot;</span>:{<span class="hljs-string">&quot;$gt&quot;</span>:<span class="hljs-number">6</span>}},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>)</span>{
   <span class="hljs-comment">//&#x67E5;&#x8BE2;&#x6240;&#x6709;nage&#x5927;&#x4E8E;6&#x7684;&#x6570;&#x636E;</span>
});

Model.find({<span class="hljs-string">&quot;age&quot;</span>:{<span class="hljs-string">&quot;$lt&quot;</span>:<span class="hljs-number">6</span>}},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>)</span>{
   <span class="hljs-comment">//&#x67E5;&#x8BE2;&#x6240;&#x6709;nage&#x5C0F;&#x4E8E;6&#x7684;&#x6570;&#x636E;</span>
});

Model.find({<span class="hljs-string">&quot;age&quot;</span>:{<span class="hljs-string">&quot;$gt&quot;</span>:<span class="hljs-number">6</span>,<span class="hljs-string">&quot;$lt&quot;</span>:<span class="hljs-number">9</span>}},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>)</span>{
  <span class="hljs-comment">//&#x67E5;&#x8BE2;&#x6240;&#x6709;nage&#x5927;&#x4E8E;6&#x5C0F;&#x4E8E;9&#x7684;&#x6570;&#x636E;</span>
});
</code></pre>
<h5 id="t253.5.6.6 $ne(&#x4E0D;&#x7B49;&#x4E8E;)">3.5.6.6 $ne(&#x4E0D;&#x7B49;&#x4E8E;) <a href="#t253.5.6.6 $ne(&#x4E0D;&#x7B49;&#x4E8E;)"> # </a></h5>
<p>$ne(!=)&#x64CD;&#x4F5C;&#x7B26;&#x7684;&#x542B;&#x4E49;&#x76F8;&#x5F53;&#x4E8E;&#x4E0D;&#x7B49;&#x4E8E;&#x3001;&#x4E0D;&#x5305;&#x542B;&#xFF0C;&#x67E5;&#x8BE2;&#x65F6;&#x6211;&#x4EEC;&#x53EF;&#x901A;&#x8FC7;&#x5B83;&#x8FDB;&#x884C;&#x6761;&#x4EF6;&#x5224;&#x5B9A;&#xFF0C;&#x5177;&#x4F53;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js">Model.find({ <span class="hljs-attr">age</span>:{ <span class="hljs-attr">$ne</span>:<span class="hljs-number">6</span>}},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>)</span>{
  <span class="hljs-comment">//&#x67E5;&#x8BE2;age&#x4E0D;&#x7B49;&#x4E8E;6&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;</span>
});
</code></pre>
<h5 id="t263.5.6.7 $in(&#x5305;&#x542B;)">3.5.6.7 $in(&#x5305;&#x542B;) <a href="#t263.5.6.7 $in(&#x5305;&#x542B;)"> # </a></h5>
<p>&#x548C;$ne&#x64CD;&#x4F5C;&#x7B26;&#x76F8;&#x53CD;&#xFF0C;$in&#x76F8;&#x5F53;&#x4E8E;&#x5305;&#x542B;&#x3001;&#x7B49;&#x4E8E;&#xFF0C;&#x67E5;&#x8BE2;&#x65F6;&#x67E5;&#x627E;&#x5305;&#x542B;&#x4E8E;&#x6307;&#x5B9A;&#x5B57;&#x6BB5;&#x6761;&#x4EF6;&#x7684;&#x6570;&#x636E;</p>
<pre><code class="lang-js">Model.find({ <span class="hljs-attr">age</span>:{ <span class="hljs-attr">$in</span>: <span class="hljs-number">6</span>}},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>)</span>{
   <span class="hljs-comment">//&#x67E5;&#x8BE2;age&#x7B49;&#x4E8E;6&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;</span>
});

Model.find({ <span class="hljs-attr">age</span>:{<span class="hljs-attr">$in</span>:[<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]}},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>)</span>{
  <span class="hljs-comment">//&#x53EF;&#x4EE5;&#x628A;&#x591A;&#x4E2A;&#x503C;&#x7EC4;&#x7EC7;&#x6210;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;</span>
});
</code></pre>
<h5 id="t273.5.6.8 $or(&#x6216;&#x8005;)">3.5.6.8 $or(&#x6216;&#x8005;) <a href="#t273.5.6.8 $or(&#x6216;&#x8005;)"> # </a></h5>
<p>&#x53EF;&#x4EE5;&#x67E5;&#x8BE2;&#x591A;&#x4E2A;&#x952E;&#x503C;&#x7684;&#x4EFB;&#x610F;&#x7ED9;&#x5B9A;&#x503C;&#xFF0C;&#x53EA;&#x8981;&#x6EE1;&#x8DB3;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x5C31;&#x53EF;&#x8FD4;&#x56DE;&#xFF0C;&#x7528;&#x4E8E;&#x5B58;&#x5728;&#x591A;&#x4E2A;&#x6761;&#x4EF6;&#x5224;&#x5B9A;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x4F7F;&#x7528;&#xFF0C;&#x5982;&#x4E0B;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js">Model.find({<span class="hljs-string">&quot;$or&quot;</span>:[{<span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;zfpx&quot;</span>},{<span class="hljs-string">&quot;age&quot;</span>:<span class="hljs-number">6</span>}]},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>)</span>{
    <span class="hljs-comment">//&#x67E5;&#x8BE2;name&#x4E3A;zfpx&#x6216;age&#x4E3A;6&#x7684;&#x5168;&#x90E8;&#x6587;&#x6863;</span>
});
</code></pre>
<h5 id="t283.5.6.9 $exists(&#x662F;&#x5426;&#x5B58;&#x5728;)">3.5.6.9 $exists(&#x662F;&#x5426;&#x5B58;&#x5728;) <a href="#t283.5.6.9 $exists(&#x662F;&#x5426;&#x5B58;&#x5728;)"> # </a></h5>
<p>$exists&#x64CD;&#x4F5C;&#x7B26;&#xFF0C;&#x53EF;&#x7528;&#x4E8E;&#x5224;&#x65AD;&#x67D0;&#x4E9B;&#x5173;&#x952E;&#x5B57;&#x6BB5;&#x662F;&#x5426;&#x5B58;&#x5728;&#x6765;&#x8FDB;&#x884C;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;&#x3002;&#x5982;&#x4E0B;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js">Model.find({<span class="hljs-attr">name</span>: {<span class="hljs-attr">$exists</span>: <span class="hljs-literal">true</span>}},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>)</span>{
      <span class="hljs-comment">//&#x67E5;&#x8BE2;&#x6240;&#x6709;&#x5B58;&#x5728;name&#x5C5E;&#x6027;&#x7684;&#x6587;&#x6863;</span>
});

Model.find({<span class="hljs-attr">email</span>: {<span class="hljs-attr">$exists</span>: <span class="hljs-literal">false</span>}},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error,docs</span>)</span>{
      <span class="hljs-comment">//&#x67E5;&#x8BE2;&#x6240;&#x6709;&#x4E0D;&#x5B58;&#x5728;email&#x5C5E;&#x6027;&#x7684;&#x6587;&#x6863;</span>
});
</code></pre>
<h4 id="t293.5.7 &#x9AD8;&#x7EA7;&#x67E5;&#x8BE2;">3.5.7 &#x9AD8;&#x7EA7;&#x67E5;&#x8BE2; <a href="#t293.5.7 &#x9AD8;&#x7EA7;&#x67E5;&#x8BE2;"> # </a></h4>
<p>&#x53EF;&#x4EE5;&#x9650;&#x5236;&#x7ED3;&#x679C;&#x7684;&#x6570;&#x91CF;,&#x8DF3;&#x8FC7;&#x90E8;&#x5206;&#x7ED3;&#x679C;,&#x6839;&#x636E;&#x4EFB;&#x610F;&#x952E;&#x5BF9;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x5404;&#x79CD;&#x6392;&#x5E8F;</p>
<p>&#x6240;&#x6709;&#x8FD9;&#x4E9B;&#x9009;&#x9879;&#x90FD;&#x8981;&#x5728;&#x67E5;&#x8BE2;&#x88AB;&#x53D1;&#x9001;&#x5230;&#x670D;&#x52A1;&#x5668;&#x4E4B;&#x524D;&#x6307;&#x5B9A;</p>
<h5 id="t303.5.7.1 limit(&#x9650;&#x5236;&#x6570;&#x91CF;)">3.5.7.1 limit(&#x9650;&#x5236;&#x6570;&#x91CF;) <a href="#t303.5.7.1 limit(&#x9650;&#x5236;&#x6570;&#x91CF;)"> # </a></h5>
<p>&#x5728;&#x67E5;&#x8BE2;&#x64CD;&#x4F5C;&#x4E2D;,&#x6709;&#x65F6;&#x6570;&#x636E;&#x91CF;&#x4F1A;&#x5F88;&#x5927;,&#x8FD9;&#x65F6;&#x6211;&#x4EEC;&#x5C31;&#x9700;&#x8981;&#x5BF9;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x7684;&#x6570;&#x91CF;&#x8FDB;&#x884C;&#x9650;&#x5236;
&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;limit&#x51FD;&#x6570;&#xFF0C;&#x901A;&#x8FC7;&#x5B83;&#x6765;&#x9650;&#x5236;&#x7ED3;&#x679C;&#x6570;&#x91CF;&#x3002;
&#x8BED;&#x6CD5;</p>
<pre><code class="lang-js">find(Conditions,fields,options,callback);
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js">Model.find({},<span class="hljs-literal">null</span>,{<span class="hljs-attr">limit</span>:<span class="hljs-number">20</span>},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,docs</span>)</span>{
        <span class="hljs-built_in">console</span>.log(docs);
});
</code></pre>
<p>&#x5982;&#x679C;&#x5339;&#x914D;&#x7684;&#x7ED3;&#x679C;&#x4E0D;&#x5230;20&#x4E2A;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x5339;&#x914D;&#x6570;&#x91CF;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;limit&#x51FD;&#x6570;&#x6307;&#x5B9A;&#x7684;&#x662F;&#x4E0A;&#x9650;&#x800C;&#x975E;&#x4E0B;&#x9650;</p>
<h5 id="t313.5.7.2 skip(&#x8DF3;&#x8FC7;/&#x7565;&#x8FC7;&#x7684;&#x6570;&#x91CF;)">3.5.7.2 skip(&#x8DF3;&#x8FC7;/&#x7565;&#x8FC7;&#x7684;&#x6570;&#x91CF;) <a href="#t313.5.7.2 skip(&#x8DF3;&#x8FC7;/&#x7565;&#x8FC7;&#x7684;&#x6570;&#x91CF;)"> # </a></h5>
<p>skip&#x51FD;&#x6570;&#x7684;&#x529F;&#x80FD;&#x662F;&#x7565;&#x8FC7;&#x6307;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x5339;&#x914D;&#x7ED3;&#x679C;&#xFF0C;&#x8FD4;&#x56DE;&#x4F59;&#x4E0B;&#x7684;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;
&#x5982;&#x4E0B;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js">find(Conditions,fields,options,callback);
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js">Model.find({},<span class="hljs-literal">null</span>,{<span class="hljs-attr">skip</span>:<span class="hljs-number">4</span>},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,docs</span>)</span>{
        <span class="hljs-built_in">console</span>.log(docs);
});
</code></pre>
<p>&#x5982;&#x679C;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x6570;&#x91CF;&#x4E2D;&#x5C11;&#x4E8E;4&#x4E2A;&#x7684;&#x8BDD;&#xFF0C;&#x5219;&#x4E0D;&#x4F1A;&#x8FD4;&#x56DE;&#x4EFB;&#x4F55;&#x7ED3;&#x679C;&#x3002;</p>
<h5 id="t323.5.7.3 sort&#x51FD;&#x6570;">3.5.7.3 sort&#x51FD;&#x6570; <a href="#t323.5.7.3 sort&#x51FD;&#x6570;"> # </a></h5>
<p>sort&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5C06;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x64CD;&#x4F5C;
&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x952E;/&#x503C;&#x5BF9;
&#x952E;&#x4EE3;&#x8868;&#x8981;&#x6392;&#x5E8F;&#x7684;&#x952E;&#x540D;,&#x503C;&#x4EE3;&#x8868;&#x6392;&#x5E8F;&#x7684;&#x65B9;&#x5411;,1&#x662F;&#x5347;&#x5E8F;,-1&#x662F;&#x964D;&#x5E8F;
&#x8BED;&#x6CD5;</p>
<pre><code class="lang-js">find(Conditions,fields,options,callback)
</code></pre>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-js"> Model.find({},<span class="hljs-literal">null</span>,{<span class="hljs-attr">sort</span>:{<span class="hljs-attr">age</span>:<span class="hljs-number">-1</span>}},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,docs</span>)</span>{
      <span class="hljs-comment">//&#x67E5;&#x8BE2;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x6309;&#x7167;age&#x964D;&#x5E8F;&#x987A;&#x5E8F;&#x8FD4;&#x56DE;&#x6570;&#x636E;docs</span>
});
</code></pre>
<p>sort&#x51FD;&#x6570;&#x53EF;&#x6839;&#x636E;&#x7528;&#x6237;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#x6709;&#x9009;&#x62E9;&#x6027;&#x7684;&#x6765;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x663E;&#x793A;&#x6570;&#x636E;&#x7ED3;&#x679C;&#x3002;</p>
<h5 id="t333.5.7.4 &#x5206;&#x9875;&#x67E5;&#x8BE2;">3.5.7.4 &#x5206;&#x9875;&#x67E5;&#x8BE2; <a href="#t333.5.7.4 &#x5206;&#x9875;&#x67E5;&#x8BE2;"> # </a></h5>
<pre><code class="lang-js">Model(<span class="hljs-string">&apos;User&apos;</span>).find({})
  .sort({<span class="hljs-attr">createAt</span>:<span class="hljs-number">-1</span>})
  .skip((pageNum<span class="hljs-number">-1</span>)*pageSize)
  .limit(pageSize)
  .populate(<span class="hljs-string">&apos;user&apos;</span>)
  .exec(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,docs</span>)</span>{
     <span class="hljs-built_in">console</span>.log(docs);
  });
</code></pre>
<h5 id="t343.5.7.5 populate">3.5.7.5 populate <a href="#t343.5.7.5 populate"> # </a></h5>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> mongoose = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;mongoose&apos;</span>);
<span class="hljs-comment">//&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;</span>
mongoose.connect(<span class="hljs-string">&apos;mongodb://localhost:27017/201606blog&apos;</span>);
<span class="hljs-comment">//&#x5B9A;&#x4E49;&#x8BFE;&#x7A0B;Schema</span>
<span class="hljs-keyword">var</span> CourseSchema = <span class="hljs-keyword">new</span> mongoose.Schema({
    <span class="hljs-attr">name</span>:<span class="hljs-built_in">String</span>
});
<span class="hljs-keyword">var</span> CourseModel = mongoose.model(<span class="hljs-string">&apos;Course&apos;</span>,CourseSchema);
<span class="hljs-keyword">var</span> PersonSchema = <span class="hljs-keyword">new</span> mongoose.Schema({
    <span class="hljs-attr">name</span>:{
        <span class="hljs-attr">type</span>:<span class="hljs-built_in">String</span>,
        <span class="hljs-attr">required</span>:<span class="hljs-literal">true</span>
    },
    <span class="hljs-comment">// &#x5916;&#x952E; &#x522B;&#x7684;&#x96C6;&#x5408;&#x7684;&#x4E3B;&#x952E;</span>
    course:{
        <span class="hljs-attr">type</span>:mongoose.Schema.Types.ObjectId,
        <span class="hljs-attr">ref</span>:<span class="hljs-string">&apos;Course&apos;</span> <span class="hljs-comment">//&#x6307;&#x660E;&#x6B64;&#x5916;&#x952E;&#x662F;&#x54EA;&#x4E2A;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x5916;&#x952E;</span>
    }
});
<span class="hljs-keyword">var</span> PersonModel = mongoose.model(<span class="hljs-string">&apos;Person&apos;</span>,PersonSchema);
CourseModel.create({<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;node.js&apos;</span>},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,course</span>)</span>{
    PersonModel.create({<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx&apos;</span>,<span class="hljs-attr">course</span>:course._id},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,doc</span>)</span>{
        <span class="hljs-built_in">console</span>.log(doc);
        PersonModel.findById(doc._id).populate(<span class="hljs-string">&apos;course&apos;</span>).exec(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,doc</span>)</span>{
            <span class="hljs-built_in">console</span>.log(doc);
        })
    })
});
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
